package com.net.protocol.message.body.sender;

import com.net.protocol.message.Message;
import com.net.protocol.util.BitUtils;

import java.io.UnsupportedEncodingException;

public class LoginASK implements Message.Body.Sender.ASK {

    private byte nameLength;//账号名长度
    private String name;//账号名
    private byte pwdLength;//密码长度
    private String pwd;//密码

    public LoginASK(String name, String pwd) {
        this.name = name;
        this.pwd = pwd;
    }

    public byte getNameLength() {
        return nameLength;
    }

    public void setNameLength(byte nameLength) {
        this.nameLength = nameLength;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public byte getPwdLength() {
        return pwdLength;
    }

    public void setPwdLength(byte pwdLength) {
        this.pwdLength = pwdLength;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    @Override
    public byte[] pack(byte[] buf, int offset) throws UnsupportedEncodingException {
        int length = this.name.getBytes("GBK").length;
        setNameLength((byte) length);
        buf[offset] = (byte) length;
        BitUtils.setBytes(buf, offset + 1, this.name.getBytes("GBK"));
        int pwdLength = this.pwd.getBytes("GBK").length;
        setPwdLength((byte) pwdLength);
        buf[offset + 1 + length] = (byte) pwdLength;
        BitUtils.setBytes(buf, offset + 2 + length, this.pwd.getBytes("GBK"));
        return buf;
    }

    @Override
    public int getSize() {
        return 2 + getNameLength() + getPwdLength();
    }
}
